﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using REVISIONMAX30.Frame;
using REVISIONMAX30.Serialization;

namespace REVISIONMAX30.Controls
{
    public partial class MyGroupList : UserControl
    {
        public MyGroupList()
        {
            InitializeComponent();
        }

        public UIHandler uiHandler { get; set; }
        public DataObjects.DataMain dataMain { get; set; }

        public void Initzialize(UIHandler pUIHandler, DataObjects.DataMain pDataMain)
        {
            this.uiHandler = pUIHandler;
            this.dataMain = pDataMain;
            this.UpdateViewer();
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            var tSelectedGuid = this.lvGroups.SelectedItems.Cast<ListViewItem>().Select(t => t.Tag).Cast<Guid>().FirstOrDefault();

            if (tSelectedGuid != null)
            {
                var tNewGroupName = this.txtNewGroupName.Text.Trim();

                if (tNewGroupName.IsEmpty())
                {
                    MessageBox.Show("Group Name must be specified.");
                }
                else if (this.dataMain.GroupList.Where(t => t.GroupName.Equals(tNewGroupName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() != null)
                {
                    MessageBox.Show("Group Name must be unique.");
                }
                else
                {
                    var tCopyGroup = this.dataMain.GroupList.Where(t => t.DataObjectGUID == tSelectedGuid).First();

                    var tNewGroup = tCopyGroup.Clone();

                    tNewGroup.GroupName = tNewGroupName;

                    tNewGroup.DataObjectGUID = Guid.NewGuid();

                    foreach (var tSourceDirectory in tNewGroup.SourceDirectoryList)
                    {
                        tSourceDirectory.DataObjectGUID = Guid.NewGuid();
                    }

                    foreach (var tSQLItem in tNewGroup.SQLItemList)
                    {
                        tSQLItem.DataObjectGUID = Guid.NewGuid();
                    }

                    foreach (var tConnectionString in tNewGroup.ConnectionStringList)
                    {
                        var tNewDataObjectGUID = Guid.NewGuid();

                        var tSQLItemList = tNewGroup.SQLItemList.Where(t => t.SelectedConnectionStringGUID == tConnectionString.DataObjectGUID);

                        foreach (var tSQLItem in tSQLItemList)
                        {
                            tSQLItem.SelectedConnectionStringGUID = tNewDataObjectGUID;
                        }

                        tConnectionString.DataObjectGUID = tNewDataObjectGUID;
                    }

                    this.dataMain.GroupList.Add(tNewGroup);

                    UpdateViewer();


                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            var tSelectedGuidList = this.lvGroups.SelectedItems.Cast<ListViewItem>().Select(t => t.Tag).Cast<Guid>().ToList();
            this.dataMain.GroupList.RemoveAll(t => tSelectedGuidList.Contains(t.DataObjectGUID));
            this.UpdateViewer();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.Edit();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            var tNewGroupName = this.txtNewGroupName.Text.Trim();

            if (tNewGroupName.IsEmpty())
            {
                MessageBox.Show("Group Name must be specified.");
            }
            else if (this.dataMain.GroupList.Where(t => t.GroupName.Equals(tNewGroupName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault() != null)
            {
                MessageBox.Show("Group Name must be unique.");
            }
            else
            {
                this.uiHandler.ViewGroup(new DataObjects.Group(this.txtNewGroupName.Text), this.dataMain);

                this.txtNewGroupName.Clear();
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            this.UpdateViewer();
        }

        private void Edit()
        {
            if (this.lvGroups.SelectedItems.Count > 0)
            {
                var tGroup = dataMain.GroupList.Where(t => t.DataObjectGUID == (Guid)this.lvGroups.SelectedItems[0].Tag).FirstOrDefault();

                this.uiHandler.ViewGroup(tGroup, this.dataMain);
            }
        }

        private void lvGroups_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        public void UpdateViewer()
        {
            this.lvGroups.Items.Clear();

            //this.dataMain.GroupList.Sort();

            foreach (var tGroup in this.dataMain.GroupList)
            {
                var tlvItem = this.lvGroups.Items.Add(tGroup.GroupName);
                tlvItem.Tag = tGroup.DataObjectGUID;
                tlvItem.SubItems.Add(tGroup.CurrentRevision.StringValueOrEmpty());
                tlvItem.SubItems.Add(tGroup.Created.StringValueOrEmpty());
                tlvItem.SubItems.Add(tGroup.Updated.StringValueOrEmpty());
            }

            if (this.lvGroups.Items.Count > 0)
            {
                foreach (ColumnHeader header in this.lvGroups.Columns)
                {
                    header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }
        }

    }
}
